package com.liuchuanzheng.lcz_wanandroid.module.main.activity

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.liuchuanzheng.lcz_wanandroid.R
import com.liuchuanzheng.lcz_wanandroid.base.BaseActivity
import com.liuchuanzheng.lcz_wanandroid.databinding.ActivityMainBinding
import com.liuchuanzheng.lcz_wanandroid.module.home.fragment.HomeFragment
import com.liuchuanzheng.lcz_wanandroid.module.mine.fragment.MineFragment

class MainActivity : BaseActivity() {
    lateinit var mViewBinding: ActivityMainBinding
    val itemIds = mutableListOf<Int>(R.id.home,R.id.mine)
    private lateinit var fragments: Map<Int, Fragment>
    /**
     * 因为系统自带的设置view方法有很多种,id,view都可以.所以这里不像
     * 大家都用id的封装方式,那是限制了自己
     * 建议用viewBinding的方式。个人不喜欢dataBinding
     */
    override fun setContentView() {
        mViewBinding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(mViewBinding.root)

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        clearToast(mViewBinding.bottomNavigationView,itemIds)
        fragments = mapOf(
            R.id.home to createFragment(HomeFragment::class.java),
            R.id.mine to createFragment(MineFragment::class.java)
        )
        mViewBinding.bottomNavigationView.run {
            setOnNavigationItemSelectedListener { menuItem ->
                showFragment(menuItem.itemId)
                true
            }
            setOnNavigationItemReselectedListener { menuItem ->

            }
        }

        if (savedInstanceState == null) {
            val initialItemId = R.id.home
            mViewBinding.bottomNavigationView.selectedItemId = initialItemId
            showFragment(initialItemId)
        }
    }
    private fun createFragment(clazz: Class<out Fragment>): Fragment {
        var fragment = supportFragmentManager.fragments.find { it.javaClass == clazz }
        if (fragment == null) {
            fragment = when (clazz) {
                HomeFragment::class.java -> HomeFragment()
                MineFragment::class.java -> MineFragment()
                else -> throw IllegalArgumentException("argument ${clazz.simpleName} is illegal")
            }
        }
        return fragment
    }
    private fun showFragment(menuItemId: Int) {
        val currentFragment = supportFragmentManager.fragments.find {
            it.isVisible && it in fragments.values
        }
        val targetFragment = fragments[menuItemId]
        supportFragmentManager.beginTransaction().apply {
            currentFragment?.let { if (it.isVisible) hide(it) }
            targetFragment?.let {
                if (it.isAdded) show(it) else add(R.id.fl, it)
            }
        }.commit()
    }
    /*
     * 清除长按时的toast
     * @param bottomNavigationView 当前BottomNavigationView
     * @param ids                  与配置文件中对应的所有id
     */
    private fun clearToast(bottomNavigationView: BottomNavigationView, ids: MutableList<Int>) {
        var bottomNavigationMenuView: ViewGroup = (bottomNavigationView.getChildAt(0) as ViewGroup)
        //遍历子View,重写长按点击事件
        for (position in 0 until ids.size) {
            bottomNavigationMenuView.getChildAt(position)
                .findViewById<View>((ids[position] as Int?)!!)
                .setOnLongClickListener { true }
        }

    }
}